home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.11 Nov 90 / Transition Source / Transition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  11.8 KB  |  441 lines  |  [TEXT/KAHL]

  1. /**************************
  2. Transition.c
  3. created by Kirk Chase 3/29/90
  4. ***************************/
  5. #include "Transition.h"
  6. /* external variables and globals */
  7. extern pascal RgnHandle MakeRgn(BitMap *srcMap); /* in 32 bit QD */
  8.  
  9. /************************************/
  10. /* routines:
  11.     • Ptr NewBitMap(bm, r)
  12.     • void DisposBitMap(bm)
  13.     • GrafPtr NewOffScreen(theBounds)
  14.     • void DisposOffScreen(offPort)
  15.     • void WipeTopBottom(srcBits, dstBits, srcRect, dstRect, mode, mask,
  16.         partitions, pauseTicks)
  17.     • void WipeBottomTop(srcBits, dstBits, srcRect, dstRect, mode, mask,
  18.         partitions, pauseTicks)
  19.     • void WipeLeftRight(srcBits, dstBits, srcRect, dstRect, mode, mask,
  20.         partitions, pauseTicks)
  21.     • void WipeRightLeft(srcBits, dstBits, srcRect, dstRect, mode, mask,
  22.         partitions, pauseTicks)
  23.     • void Iris(srcBits, dstBits, srcRect, dstRect, mode, mask,
  24.         irisRgn, pauseTicks)
  25.     • void FadePat(srcBits, dstBits, srcRect, dstRect, mode, mask,
  26.         patList, patCount, pauseTicks)
  27.     • void PageFlipDown(srcBits, dstBits, srcRect, dstRect, mask,
  28.         partitions, pauseTicks)
  29.     • void PageFlipRight(srcBits, dstBits, srcRect, dstRect, mask,
  30.         partitions, pauseTicks)
  31.     */
  32. /************************************/
  33.  
  34. /************************************/
  35. /* Ptr NewBitMap()
  36.     creates a bitmap */
  37. /************************************/
  38. Ptr NewBitMap(bm, r)
  39. BitMap *bm;
  40. Rect *r;
  41. { /* NewBitMap() */
  42. bm->rowBytes = ((r->right - r->left + 15) / 16) * 2;
  43. bm->bounds = *r;
  44. bm->baseAddr = NewPtr(bm->rowBytes * (long) (r->right - r->left));
  45. if (MemError() != noErr) return (0L);
  46. else return (bm->baseAddr);
  47. } /* NewBitMap() */
  48.  
  49. /************************************/
  50. /* void DisposBitMap()
  51.     disposes of a bitmap */
  52. /************************************/
  53. void DisposBitMap(bm)
  54. BitMap *bm;
  55. { /* DisposBitMap() */
  56. DisposPtr(bm->baseAddr);
  57. SetRect(&bm->bounds,0,0,0,0);
  58. bm->rowBytes=0;
  59. bm->baseAddr=0L;
  60. } /* DisposBitMap() */
  61.  
  62. /************************************/
  63. /* GrafPtr NewOffScreen()
  64.     creates an offscreen grafport */
  65. /************************************/
  66. GrafPtr NewOffScreen(theBounds)
  67. Rect *theBounds;
  68. { /* NewOffScreen() */
  69. GrafPtr oldPort, offPort;
  70.  
  71. /* allocate port memory */
  72. offPort = (GrafPtr) NewPtr(sizeof(GrafPort));
  73. if (MemError() != noErr)
  74.     return (0L);
  75.  
  76. /* open port and set port variables */
  77. GetPort(&oldPort);
  78. OpenPort(offPort);
  79. offPort->portRect = *theBounds;
  80.  
  81. if (NewBitMap(&(offPort->portBits), theBounds) == (0L)) {
  82.     ClosePort(offPort);
  83.     SetPort(oldPort);
  84.     DisposPtr((Ptr) offPort);
  85.     return(0L);
  86.     }
  87.  
  88. RectRgn(offPort->clipRgn, theBounds);
  89. RectRgn(offPort->visRgn, theBounds);
  90.  
  91. EraseRect(theBounds);
  92.  
  93. SetPort(oldPort);
  94.  
  95. return(offPort);
  96.  
  97.  
  98. } /* NewOffScreen() */
  99.  
  100. /************************************/
  101. /* void DisposOffScreen()
  102.     destroys an offscreen grafport */
  103. /************************************/
  104. void DisposOffScreen(offPort)
  105. GrafPtr offPort;
  106. { /* DisposOffScreen() */
  107. ClosePort(offPort);
  108. DisposBitMap(&(offPort->portBits));
  109. DisposPtr((Ptr) offPort);
  110. } /* DisposOffScreen() */
  111.  
  112. /************************************/
  113. /* void WipeTopBottom()
  114.     WipeTopBottom uses CopyBits to transfer one image on top of another
  115. in segments called partitions with an optional pause */
  116. /************************************/
  117. void WipeTopBottom(srcBits, dstBits, srcRect, dstRect, mode, mask,
  118.     partitions, pauseTicks)
  119. BitMap *srcBits, *dstBits;
  120. Rect *srcRect, *dstRect;
  121. int mode;
  122. RgnHandle mask;
  123. int partitions;
  124. long pauseTicks;
  125. { /*WipeTopBottom */
  126. int width,i, j;
  127. Rect partSRect, partDRect;
  128. long ticks;
  129.  
  130. width = (srcRect->bottom - srcRect->top) / partitions;
  131. while ((partitions * width) < (srcRect->bottom - srcRect->top)) width += 1;
  132.  
  133. for (j=0; j<width; j++) { /* increment pane */
  134.     ticks = TickCount();
  135.     
  136.     for (i=0; i<partitions; i++) { /* each pane */
  137.         SetRect(&partSRect, srcRect->left,
  138.                 (srcRect->top) + (i * width) + j,
  139.                 srcRect->right,
  140.                 (srcRect->top) + (i * width) + j + 1);
  141.                 
  142.         if (partSRect.bottom > srcRect->bottom) partSRect.bottom = srcRect->bottom;
  143.         
  144.         partDRect = partSRect;
  145.         MapRect(&partDRect, srcRect, dstRect);
  146.         
  147.         CopyBits(srcBits, dstBits, &partSRect, &partDRect, mode, mask);
  148.         } /* each pane */
  149.         
  150.     while (TickCount() < ticks + pauseTicks) ;
  151.     } /* increment pane */
  152.     
  153. } /*WipeTopBottom */
  154.  
  155. /************************************/
  156. /* void WipeBottomTop()
  157.     WipeBottomTop uses CopyBits to transfer one image on top of another
  158. in segments called partitions with an optional pause */
  159. /************************************/
  160. void WipeBottomTop(srcBits, dstBits, srcRect, dstRect, mode, mask,
  161.     partitions, pauseTicks)
  162. BitMap *srcBits, *dstBits;
  163. Rect *srcRect, *dstRect;
  164. int mode;
  165. RgnHandle mask;
  166. int partitions;
  167. long pauseTicks;
  168. { /*WipeBottomTop */
  169. int width,i, j;
  170. Rect partSRect, partDRect;
  171. long ticks;
  172.  
  173. width = (srcRect->bottom - srcRect->top) / partitions;
  174. while ((partitions * width) < (srcRect->bottom - srcRect->top)) width += 1;
  175.  
  176. for (j=0; j<width; j++) { /* increment pane */
  177.     ticks = TickCount();
  178.     
  179.     for (i=0; i<partitions; i++) { /* each pane */
  180.         SetRect(&partSRect, srcRect->left,
  181.                 (srcRect->bottom) - (i * width) - j - 1,
  182.                 srcRect->right,
  183.                 (srcRect->bottom) - (i * width) - j);
  184.                 
  185.         if (partSRect.top < srcRect->top) partSRect.top = srcRect->top;
  186.         
  187.         partDRect = partSRect;
  188.         MapRect(&partDRect, srcRect, dstRect);
  189.         
  190.         CopyBits(srcBits, dstBits, &partSRect, &partDRect, mode, mask);
  191.         } /* each pane */
  192.         
  193.     while (TickCount() < ticks + pauseTicks) ;
  194.     } /* increment pane */
  195.     
  196. } /*WipeBottomTop */
  197.  
  198. /************************************/
  199. /* void WipeLeftRight()
  200.     WipeLeftRight uses CopyBits to transfer one image on top of another
  201. in segments called partitions with an optional pause */
  202. /************************************/
  203. void WipeLeftRight(srcBits, dstBits, srcRect, dstRect, mode, mask,
  204.     partitions, pauseTicks)
  205. BitMap *srcBits, *dstBits;
  206. Rect *srcRect, *dstRect;
  207. int mode;
  208. RgnHandle mask;
  209. int partitions;
  210. long pauseTicks;
  211. { /*WipeLeftRight */
  212. int width,i, j;
  213. Rect partSRect, partDRect;
  214. long ticks;
  215.  
  216. width = (srcRect->right - srcRect->left) / partitions;
  217. while ((partitions * width) < (srcRect->right - srcRect->left)) width += 1;
  218.  
  219. for (j=0; j<width; j++) { /* increment pane */
  220.     ticks = TickCount();
  221.     
  222.     for (i=0; i<partitions; i++) { /* each pane */
  223.         SetRect(&partSRect, srcRect->left + (i * width) + j,
  224.                 srcRect->top,
  225.                 srcRect->left + (i * width) + j + 1,
  226.                 srcRect->bottom);
  227.                 
  228.         if (partSRect.right > srcRect->right) partSRect.right = srcRect->right;
  229.         
  230.         partDRect = partSRect;
  231.         MapRect(&partDRect, srcRect, dstRect);
  232.         
  233.         CopyBits(srcBits, dstBits, &partSRect, &partDRect, mode, mask);
  234.         } /* each pane */
  235.         
  236.     while (TickCount() < ticks + pauseTicks) ;
  237.     } /* increment pane */
  238.     
  239. } /*WipeLeftRight */
  240.  
  241. /************************************/
  242. /* void WipeRightLeft()
  243.     WipeRightLeft uses CopyBits to transfer one image on top of another
  244. in segments called partitions with an optional pause */
  245. /************************************/
  246. void WipeRightLeft(srcBits, dstBits, srcRect, dstRect, mode, mask,
  247.     partitions, pauseTicks)
  248. BitMap *srcBits, *dstBits;
  249. Rect *srcRect, *dstRect;
  250. int mode;
  251. RgnHandle mask;
  252. int partitions;
  253. long pauseTicks;
  254. { /*WipeRightLeft */
  255. int width,i, j;
  256. Rect partSRect, partDRect;
  257. long ticks;
  258.  
  259. width = (srcRect->right - srcRect->left) / partitions;
  260. while ((partitions * width) < (srcRect->right - srcRect->left)) width += 1;
  261.  
  262. for (j=0; j<width; j++) { /* increment pane */
  263.     ticks = TickCount();
  264.     
  265.     for (i=0; i<partitions; i++) { /* each pane */
  266.         SetRect(&partSRect, srcRect->right - (i * width) - j - 1,
  267.                 srcRect->top,
  268.                 srcRect->right - (i * width) - j,
  269.                 srcRect->bottom);
  270.                 
  271.         if (partSRect.left < srcRect->left) partSRect.left = srcRect->left;
  272.         
  273.         partDRect = partSRect;
  274.         MapRect(&partDRect, srcRect, dstRect);
  275.         
  276.         CopyBits(srcBits, dstBits, &partSRect, &partDRect, mode, mask);
  277.         } /* each pane */
  278.         
  279.     while (TickCount() < ticks + pauseTicks);
  280.     } /* increment pane */
  281.     
  282. } /*WipeRightLeft */
  283.  
  284. /************************************/
  285. /* void Iris()
  286.     Iris uses CopyBits to transfer one image on top of another
  287. using an iris region with an optional pause */
  288. /************************************/
  289. void Iris(srcBits, dstBits, srcRect, dstRect, mode, mask,
  290.     irisRgn, pauseTicks)
  291. BitMap *srcBits, *dstBits;
  292. Rect *srcRect, *dstRect;
  293. int mode;
  294. RgnHandle mask;
  295. long pauseTicks;
  296. RgnHandle irisRgn;
  297. { /* Iris() */
  298. long ticks;
  299. RgnHandle theMask, sumRgn;
  300. RgnHandle iris;
  301.  
  302. /* prepare iris */
  303. iris = NewRgn();
  304. CopyRgn(irisRgn, iris);
  305.  
  306. sumRgn = NewRgn();
  307. SetEmptyRgn(sumRgn);
  308.  
  309. theMask = NewRgn();
  310.  
  311. while (!EqualRgn(mask, sumRgn)) {
  312.     SectRgn(iris, mask, theMask);
  313.     ticks = TickCount();
  314.     CopyBits(srcBits, dstBits, srcRect, dstRect, mode, theMask);
  315.     while (TickCount() < ticks + pauseTicks);
  316.     
  317.     /* create new iris region */
  318.     UnionRgn(sumRgn, theMask, sumRgn);
  319.     InsetRgn(iris, -1, -1);
  320.     DiffRgn(iris, sumRgn, iris);
  321.     } /* while */
  322.     
  323. DisposeRgn(sumRgn);
  324. DisposeRgn(iris);
  325. DisposeRgn(theMask);
  326. } /* Iris() */
  327.  
  328. /************************************/
  329. /* void FadePat()
  330.     FadePat uses CopyBits to transfer one image on top of another
  331. using a series of patterns with an optional pause */
  332. /************************************/
  333. void FadePat(srcBits, dstBits, srcRect, dstRect, mode, mask,
  334.     patList, patCount, pauseTicks)
  335. BitMap *srcBits, *dstBits;
  336. Rect *srcRect, *dstRect;
  337. int mode;
  338. RgnHandle mask;
  339. long pauseTicks;
  340. int patList;
  341. int patCount;
  342. { /* FadePat() */
  343. int i;
  344. long ticks;
  345. Pattern pat;
  346. BitMap bm, *bmPtr;
  347. Ptr p;
  348. GrafPort gp, *savePort;
  349. RgnHandle theRgn;
  350.  
  351. p = NewBitMap(&bm, srcRect);
  352. if (p == 0L) return;
  353.  
  354. bmPtr = (BitMap *) &bm;
  355.  
  356. GetPort(&savePort);
  357. OpenPort(&gp);
  358. SetPortBits(bmPtr);
  359. RectRgn(gp.clipRgn, &(bmPtr->bounds));
  360. RectRgn(gp.visRgn, &(bmPtr->bounds));
  361. gp.portRect = bmPtr->bounds;
  362.  
  363. for (i=1; i<= patCount; i++) { /* pattern loop */
  364.     GetIndPattern(&pat, patList, i);
  365.     PenPat(pat);
  366.     PaintRect(srcRect);
  367.     theRgn = MakeRgn(bmPtr);
  368.     ticks = TickCount();
  369.     CopyBits(srcBits, bmPtr, srcRect, srcRect, srcCopy, theRgn);
  370.     
  371.     MapRgn(theRgn, srcRect, dstRect);
  372.     CopyBits(bmPtr, dstBits, srcRect, dstRect, mode, theRgn);
  373.     while (TickCount() < ticks + pauseTicks);
  374.     DisposeRgn(theRgn);
  375.     } /* pattern loop */
  376.     
  377. ClosePort(&gp);
  378. SetPort(savePort);
  379.  
  380. DisposBitMap(&bm);
  381. } /* FadePat() */
  382.  
  383. /************************************/
  384. /* void PageFlipDown()
  385.     PageFlipDown uses CopyBits to transfer one image on top of another
  386. (only in srcCopy Mode) with an optional pause */
  387. /************************************/
  388. void PageFlipDown(srcBits, dstBits, srcRect, dstRect, mask,
  389.     partitions, pauseTicks)
  390. BitMap *srcBits, *dstBits;
  391. Rect *srcRect, *dstRect;
  392. RgnHandle mask;
  393. int partitions;
  394. long pauseTicks;
  395. { /* PageFlipDown() */
  396. Rect tempRect = *dstRect;
  397. char done = 0;
  398. long ticks;
  399.  
  400. tempRect.bottom = tempRect.top + partitions;
  401. if (tempRect.bottom > dstRect->bottom) tempRect.bottom = dstRect->bottom;
  402. while (!done) {
  403.     ticks = TickCount();
  404.     CopyBits(srcBits, dstBits, srcRect, &tempRect, srcCopy, mask);
  405.     while (TickCount() < ticks + pauseTicks);
  406.     
  407.     if (tempRect.bottom == dstRect->bottom) done = 1;
  408.     tempRect.bottom = tempRect.bottom + partitions;
  409.     if (tempRect.bottom > dstRect->bottom) tempRect.bottom = dstRect->bottom;
  410.     }
  411. } /* PageFlipDown() */
  412.  
  413. /************************************/
  414. /* void PageFlipRight()
  415.     PageFlipRight uses CopyBits to transfer one image on top of another
  416. (only in srcCopy Mode) with an optional pause */
  417. /************************************/
  418. void PageFlipRight(srcBits, dstBits, srcRect, dstRect, mask,
  419.     partitions, pauseTicks)
  420. BitMap *srcBits, *dstBits;
  421. Rect *srcRect, *dstRect;
  422. RgnHandle mask;
  423. int partitions;
  424. long pauseTicks;
  425. { /* PageFlipRight() */
  426. Rect tempRect = *dstRect;
  427. char done = 0;
  428. long ticks;
  429.  
  430. tempRect.right = tempRect.left + partitions;
  431. if (tempRect.right > dstRect->right) tempRect.right = dstRect->right;
  432. while (!done) {
  433.     ticks = TickCount();
  434.     CopyBits(srcBits, dstBits, srcRect, &tempRect, srcCopy, mask);
  435.     while (TickCount() < ticks + pauseTicks);
  436.     
  437.     if (tempRect.right == dstRect->right) done = 1;
  438.     tempRect.right = tempRect.right + partitions;
  439.     if (tempRect.right > dstRect->right) tempRect.right = dstRect->right;
  440.     }
  441. } /* PageFlipRight() */